package com.msr.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msr.mapper.ArticleMapper;
import com.msr.pojo.Article;
import com.msr.pojo.entity.PageBean;
import com.msr.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.service
 * @date 2025/1/28 9:36
 * @Copyright © 株式会社多言语系统研究所
 */
@Service
public class ArticleServiceImpl implements ArticleService{
    @Autowired
    private ArticleMapper articleMapper;

    //添加文章
    @Override
    public void addArticle(Article article) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer userId =(Integer) claims.get("id");  //用户id
        article.setCreateUser(userId);
        articleMapper.addArticle(article);
    }
    //分页文章
    @Override
    public PageBean<Article> listPage(Integer pageNum, Integer pageSize, String categoryId, String state) {
        //1、开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //2、调用mapper分页参数
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer userId =(Integer) claims.get("id");  //用户id
        List<Article> articleList = articleMapper.listPage(userId,categoryId,state);
        //3、根据集合数据获取分页对象
        Page<Article> page = (Page<Article>)articleList;
        //4、把查询的数据封装到PageBean中
        PageBean<Article> pageBean = new PageBean<Article>(page.getTotal(),page.getResult());
        return pageBean;
    }
}
